/**
 * 删除/置为无效 原因选择
 * 2025-09-17 15:20:38 tzj
 * */
import {Modal, Select, Form, Input, message} from "antd";
import React from "react";
import Dictionary from "../../services/dictionary";

class LzModal extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            title: "",
            options: [],
            // 作废原因编号
            zfyybh: null,
            // 作废原因
            zfyy: "",
            record:null
        }
    }

    open(title,record) {
        this.setState({
            title,
            record,
            visible: true
        })
        this.getOptions()
    }

    close() {
        this.setState({
            visible: false
        })
    }

    handleOk = () => {
        const {zfyybh,zfyy,record} = this.state;
        if(!zfyybh) {
            message.warn("请选择作废原因")
        }else if(!zfyy) {
            message.warn("请选择其他作废原因")
        }else {
            if (this.props.confirmCb) this.props.confirmCb({
                zfyybh, zfyy
            }, record)
        }
    }

    handleCancel = () => {
        this.setState({
            visible: false
        })
    }

    getOptions() {
        Dictionary.getDelReason().then(data => {
            this.setState({
                options: data
            })
        })
    }

    onChange = (_, e) => {
        this.setState({
            zfyy: e.value === 6 ? "" : e.children,
            zfyybh: e.value
        })
    }

    otherChange = (e) => {
        this.setState({
            zfyy: e.target.value
        })
    }

    render() {
        const {title, options, zfyybh} = this.state;
        return (
            <Modal
                title="选择原因"
                visible={this.state.visible}
                onOk={this.handleOk}
                onCancel={this.handleCancel}
            >
                <p>{title}</p>
                <Form.Item label="原因" required={true} name="zfyybh">
                    <Select showSearch={true} placeholder="请选择删除原因" onChange={this.onChange}>
                        {options.map(item => {
                            return (
                                <Select.Option key={item.value} value={item.value}>
                                    {item.label}
                                </Select.Option>
                            )
                        })}
                    </Select>
                </Form.Item>
                {
                    zfyybh === 6 ? <Form.Item label="其他原因" required={true} name="zfyy">
                        <Input placeholder="请输入其他原因" onChange={this.otherChange}></Input>
                    </Form.Item> : null
                }
            </Modal>
        )
    }
}

export default LzModal;